<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */
//用户端登陆
//基本思路：当需要用户登陆方可访问时，跳转至此页。先检测用户环境（微信端、手机端、pc端、小程序端）
//微信端和小程序端统一处理：如果存在$_SESSION['openid']则根据openid来登陆
//微信端如果没能获取到openid，则类比普通手机端处理
//普通手机端和pc端统一处理
//总结精炼：登陆（注册）前不判断，登陆（注册）后，如果session中有openid，则把当前粉丝与登陆的用户绑定
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$return['account']=$_SESSION['account'];
$return['extradata']=array();
$setting=uni_setting('register');
$return['name_title']=$setting['name_mobile']?'手机号':'账号';
$_SESSION['isauth_base']=0;
if($_RQ['op']=='get'){
	$return['item']=array();
	$return['title']='注册';
	$item=array();
	$item['show_type']=$setting['login_type']==2?true:false;
	$item['login_type']=$setting['login_type']<2?$setting['login_type']:0;
	$return['setting']=$setting;
	$item['xieyi_title']=$setting['xieyi_title'];
	$item['user_xieyi']=$setting['user_xieyi'];
	$item['fromcode']=$_SESSION['fromuser']?:'';
	$return['item']=$item;
	set_html('title','注册会员');
	exi($return);
}
if($_RQ['op']=='sendcode'){
	$post=get_postdata();
	$mobile=$post['mobile'];
	$code=rand(100000,999999);
	$verify=array(
		'mobile'=>$mobile,
		'code'=>$code,
	);
	buffer_save($verify,'core_sms_verify');
	$content=array('code'=>$code);
	$setting=uni_setting('register');
	$template_id=$setting['dxmb'];
	if(!$template_id){
		exi('管理员未设置系统短信验证码模版，请联系管理员设置(站点设置--站点注册设置--验证码短信模版ID)！','error');
	}
	$result=cfc('core')->sendsms($mobile,$content,$template_id);
	if(is_error($result)){
		exi('验证码发送失败：'.$result['message'],'error');
	}
	if(!$result){
		exi('error');
	}else{
		exi('success');
	}
}
if($_RQ['op']=='register'){
	//注册
	if($_SESSION['uid']){
		model('core/member')->logout($_SESSION['uid']);
	}
	$post=get_postdata();
	$login_type=intval($post['login_type']);
	$login_type=$login_type==2?0:$login_type;
	if($_SESSION['uid']){
		exi('您已登录！无需再注册！','error');
	}
	if($login_type==1){
		//手机验证码注册
		if(!check_sms_code($post['smscode'])){
			exi('手机验证码错误！','error');
		}
		$uid=pdo_getcolumn('core_members',array('mobile'=>trim($post['mobile']),'uniacid'=>$_SESSION['uniacid']),'id');
		if($uid){
			exi('手机号已存在，请更换!','error');
		}
		$member=array('mobile'=>trim($post['mobile']),'realmobile'=>trim($post['mobile']));
	}elseif($login_type==0){
		//账号密码注册
		if(!$post['username']){
			exi('请输入'.$return['name_title'].'！','error');
		}
        if(!preg_match("/^1[3456789]{1}\d{9}$/",$post['username']) && $setting['name_mobile']){
            exi('请输入正确的手机号码','error');
        }
		if($post['username'] == $post['password']){
			exi($return['name_title'].'跟密码不能相同！','error');
		}
		if(!$post['password'] || !$post['password2']){
			exi('请输入密码！','error');
		}
		if($post['password'] != $post['password2']){
			exi('两次输入密码不一致！','error');
		}
		$uid=pdo_getcolumn('core_members',array('username'=>$post['username'],'uniacid'=>$_SESSION['uniacid']),'id');
		if($uid){
			exi($return['name_title'].'已存在，请更换!','error');
		}
		$member=array('username'=>trim($post['username']),'password'=>trim($post['password']));
        if($setting['name_mobile']){
            $member['mobile']=trim($post['username']);
        }
	}
	if($setting['show_needcode'] && $post['fromcode']){
		$fromcode=pdo_getcolumn('core_members',array('id'=>trim($post['fromcode']),'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$fromcode){
			$fromcode=pdo_getcolumn('core_members',array('snmid'=>trim($post['fromcode']),'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$fromcode && $post['fromcode']){
				exi('邀请码不存在，不能注册!','error');
			}
		}
	}
	if($setting['show_needcode'] && !$post['fromcode']){
		exi('请填写邀请码!','error');
	}
	if($post['check']!=1 && $post['needcheck']){
		exi('请先阅读协议！','error');
	}
	$member['fromuser']=$member['fromcode']=$fromcode;
	$member['nickname']=$member['mobile']?:$member['username'];
	$uid=model('core/member')->register($member,'',$setting['must_check']);
	if($setting['must_check']==1){
		exi('等待审核！','error');
	}
	model('core/member')->login($uid);
	if($_SESSION['forward_url']){
		$url=$_SESSION['forward_url'];
	}else{
		$url='/member.home/';
	}
	exi('注册成功！','success',$url);
}
if($_RQ['op']=='upfromuser'){
	$post=get_postdata();
	if(!$post['fromuser']){
		exi('请输入邀请码！','error');
	}
	$fromuser=pdo_getcolumn('core_members',array('id'=>trim($post['fromuser']),'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$fromuser){
		exi('邀请码不存在，请检查!','error');
	}
	pdo_update('core_members',array('fromuser'=>$fromuser),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$_SESSION['member']['fromuser']=$fromuser;
	publish('core','member.setfromuser',array('mid'=>$_SESSION['uid']));
	exi('success');
}
if($_RQ['op']=='qq_login'){
	if(!$_AR['system']['qq_login']){
		exi('系统暂不支持QQ登陆！','error','./index.php');
	}
	if($_RQ['step']=='back'){
		if($_RQ['openid']){
			//获得openid，为用户登陆
			$openid=trim($_RQ['openid']);
			$uniacid=pdo_getcolumn('core_open_user',array('open_id'=>'1002','open_uid'=>$openid),'uniacid');
			if(!$uniacid){
				$setting=core_setting('zhuce');
				if(!$setting['open']){
					exi('当前QQ尚未绑定本站账号！系统自动注册失败:本站暂未开启注册！','error','./index.php');
				}
				do{
					$username='qq_'.random(10);
					$check=pdo_getcolumn('core_users',array('name'=>$username),'id');
				}while($check);
				$member=array('name'=>$username);
				$uniacid=cfc('user')->create_user($member);
				$user=array(
					'uniacid'=>$uniacid,
					'open_id'=>'1002',
					'open_uid'=>$openid,
				);
				pdo_insert('core_open_user',$user);
			}
			//登陆用户
			cfc('user')->login($uniacid);
			//跳转首页
			$_SESSION['access']=array();
			exi('授权登陆成功！','','./index.php');
		}else{
			$_SESSION['access']=array();
			exi('授权登录失败！','','./index.php');
		}
		
	}
	//微信授权登陆
	$back_url=SITEROOT.'custom/index.php?plugin=core&action=member.login&op=qq_login&step=back';
	$url=cloud_request('cloud.login.qq',array('back_url'=>$back_url));
	exi('','',$url);
}
if($_RQ['op']=='getmember'){
	$fields=array('id','avatar','nickname','username','mobile');
	$tomember=pdo_get('core_members',array('id'=>trim($_RQ['fromcode']),'uniacid'=>$_SESSION['uniacid']),$fields);
	if(!$tomember){
		$tomember=pdo_get('core_members',array('snmid'=>trim($_RQ['fromcode']),'uniacid'=>$_SESSION['uniacid']),$fields);
		if(!$tomember){
			exi('邀请码不存在!','error');
		}
	}
	$return['tomember']=$tomember;
	exi($return);
}
?>